<?php
require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
require_once($_SERVER['DOCUMENT_ROOT']."/Modules/MF93FL27/modconf.mex");
MYTCONN();
	$qr_1 = '';
	$qr_2 = '';
	$qr_3 = '';
	$qr_4 = '';
	$qr_5 = '';
	$qr_6 = '';
	$qr_7 = '';
	$qr_8 = '';
	if ($_POST['bss_Office'] > 0) {
		$qr_1 = 'AND bns_Origen1 = '.$_POST['bss_Office'];
		$qr_2 = 'AND bss_Office = '.$_POST['bss_Office'];
		$qr_3 = 'AND bpk_ROffice = '.$_POST['bss_Office'];
		$qr_7 = 'AND bpk_DOffice = '.$_POST['bss_Office'];
	}
	if ($_POST['bss_Product'] > 0) {
		$qr_4 = 'AND bns_Product = '.$_POST['bss_Product'];
		$qr_5 = 'AND bss_Product = '.$_POST['bss_Product'];
		$qr_6 = 'AND bpk_IDProduct = '.$_POST['bss_Product'];
		$qr_8 = 'AND bpk_IDProduct = '.$_POST['bss_Product'];
	}
  $GET1 = $GLOBALS['MYT']->db_array("SELECT 0 AS OID,
  	'' AS COL1,
	NAME AS COL2,
	'' AS COL3,
	OriNombre AS COL4,
	'' AS COL5,
	 bns_Origen1 AS COFF, bns_Product AS POFF,
	SUM(IF((bns_Amount/bns_Value)<1,0-(bns_Amount/bns_Value),(bns_Amount/bns_Value))) AS COL7
	FROM bonusproducts
	INNER JOIN bonusinvoice ON bns_Product = bonusproducts.REF
	INNER JOIN origenes ON OriNumero = bns_Origen1
	WHERE bns_Type = 6 AND bns_Status = 2 {$qr_1} {$qr_4}
	GROUP BY bns_Origen1, bns_Product
	ORDER BY bns_Origen1, bns_Product");

  $GET2 = $GLOBALS['MYT']->db_array("SELECT
  		bss_Office AS COFF,
  		bss_Product AS POFF,
  		SUM(bss_IniQuant) AS VAL1,
		SUM(bss_CrrQuant) AS VAL2
	FROM bonus_productstocks
	WHERE bss_Status IN (2,3,4,6) {$qr_2} {$qr_5}
	GROUP BY bss_Office, bss_Product
	ORDER BY  bss_Office, bss_Product");

  $GET3 = $GLOBALS['MYT']->db_array("SELECT
  		bpk_ROffice AS COFF,
  		bpk_IDProduct AS POFF,
		SUM(bpk_DQuant) AS VAL3
	FROM bonus_productstkmove
	WHERE bpk_Status IN (2,3,4,6) {$qr_3} {$qr_6}
	GROUP BY bpk_ROffice, bpk_IDProduct
	ORDER BY bpk_ROffice, bpk_IDProduct");

  $GET4 = $GLOBALS['MYT']->db_array("SELECT
  		bpk_DOffice AS COFF,
  		bpk_IDProduct AS POFF,
		SUM(bpk_DQuant) AS VAL4
	FROM bonus_productstkmove
	WHERE bpk_Status IN (2,3,4,6) {$qr_7} {$qr_8}
	GROUP BY bpk_DOffice, bpk_IDProduct
	ORDER BY bpk_DOffice, bpk_IDProduct");
	$CARD = $GET1[0]; ?>
<script charset="utf-8" type="text/javascript">
Ext.onReady(function(){

    Ext.QuickTips.init();

    var myData = [
	<?php
	$aa = 1;
	$bb = 1;
	$cc = 1;

	for ($i=1;$i<=$CARD;$i++) {

	$GT1 = 0;
	$GT2 = 0;
	$GT3 = 0;
	$GT4 = 0;

		for($a=$aa;$a<=$GET2[0];$a++) {
		if($GET2[$a]['COFF'] == $GET1[$i]['COFF'] && $GET2[$a]['POFF'] == $GET1[$i]['POFF']){
		$GT1 = $GET2[$a]['VAL1'];
		$GT2 = $GET2[$a]['VAL2'];
		$aa = $a;
		$a = $GET2[0];}

		}
		for($b=$bb;$b<=$GET3[0];$b++) {
		if($GET3[$b]['COFF'] == $GET1[$i]['COFF'] && $GET3[$b]['POFF'] == $GET1[$i]['POFF']){
		$GT3 = $GET3[$b]['VAL3'];
		$bb = $b;
		$b = $GET3[0];}
		}
		for($c=$cc;$c<=$GET4[0];$c++) {
		if($GET4[$c]['COFF'] == $GET1[$i]['COFF'] && $GET4[$c]['POFF'] == $GET1[$i]['POFF']){
		$GT4 = $GET4[$c]['VAL4'];
		$cc = $c;
		$c = $GET4[0];}
		}
	$GT5 = $GT1-$GET1[$i]['COL7']+$GT3-$GT4;
	echo '['.$i.','.$GET1[$i]['OID'].',\''.$GET1[$i]['COL1'].'\',\''.$GET1[$i]['COL2'].'\',\''.$GET1[$i]['COL3'].'\',\''.$GET1[$i]['COL4'].'\',\''.$GET1[$i]['COL5'].'\',\''.$GT1.'\',\''.round($GET1[$i]['COL7'],0).'\',\''.$GT3.'\',\''.$GT4.'\',\''.$GT2.'\',\''.$GT5.'\','.($GT5-$GT2).']';
		if ($i<$CARD) {
			echo ',';
		}
    } ?>
    ];

    var ds = new Ext.data.Store({
        reader: new Ext.data.ArrayReader({}, [
               {name: 'id'},
			   {name: 'OID', type: 'int'},
               {name: 'COL1', type: 'string'},
               {name: 'COL2', type: 'string'},
               {name: 'COL3', type: 'string'},
			   {name: 'COL4', type: 'string'},
               {name: 'COL5', type: 'string'},
               {name: 'COL6', type: 'string'},
               {name: 'COL7', type: 'string'},
               {name: 'COL8', type: 'string'},
               {name: 'COL9', type: 'string'},
               {name: 'COL10', type: 'string'},
               {name: 'COL11', type: 'string'},
               {name: 'COL12', type: 'int'}
          ])
    });
    ds.loadData(myData);
    var colModel = new Ext.grid.ColumnModel([
        {header: "Entrega", width: 80, sortable: true, locked:true, dataIndex: 'OID'},
        {header: "Referencia", width: 80, sortable: true, locked:true, dataIndex: 'COL1'},
        {header: "Produto", width: 150, sortable: true, dataIndex: 'COL2'},
        {header: "Data", width: 60, sortable: true, locked:true, align: 'right', dataIndex: 'COL3'},
        {header: "Loja", width: 200, sortable: true, locked:true, dataIndex: 'COL4'},
        {header: "Estado", width: 40, sortable: true, locked:true, align: 'right',dataIndex: 'COL5'},
        {header: "Recebidos", width: 75, sortable: true, locked:true, align: 'right',dataIndex: 'COL6'},
        {header: "Entregues", width: 75, sortable: true, locked:true, align: 'right',dataIndex: 'COL7'},
        {header: "Pedidos", width: 75, sortable: true, locked:true, align: 'right',dataIndex: 'COL8'},
        {header: "Encaminhados", width: 75, sortable: true, locked:true, align: 'right',dataIndex: 'COL9'},
        {header: "Stock Em Sistema", width: 75, sortable: true, locked:true, align: 'right',dataIndex: 'COL10'},
        {header: "Stock Calculado", width: 75, sortable: true, locked:true, align: 'right',dataIndex: 'COL11'},
        {header: "Diferenca", width: 75, sortable: true, locked:true, align: 'right',dataIndex: 'COL12'}
    ]);

   var selmode = new Ext.grid.RowSelectionModel({});
   var Console = new Ext.grid.GridPanel({
		ds: ds,
		cm: colModel,
		sm: selmode,
			listeners: {
	 				rowdblclick: function(ds, row) {
					var record = ds.getStore().getAt(row);
  					var value = record.get('OID');
					modalwindow('Confirmar','<?php echo $MODURL;?>lib/stk_dstentry.mex?RSID='+value,600,400);
				}
			},
	    height: 420,
        renderTo: 'bss_stockresults'
    });

});
</script>